We are migrating the bug tracker to github Issues. This is now the preferred way to report NASM bugs.

Self-registration is disabled due to spam issue (mail gorcunov@gmail.com or hpa@zytor.com to create an account)

Bug 3392604 - git nasm: segmentation fault when lmacros numdef is used
Summary: git nasm: segmentation fault when lmacros numdef is used
Status: RESOLVED FIXED
Alias: None
Product: NASM
Classification: Unclassified
Component: Assembler (show other bugs)
Version: 2.15.xx
Hardware: All All
: Medium blocker
Assignee: nobody
URL:
Depends on:
Blocks:
 
Reported: 2019-08-20 05:32 PDT by E. C. Masloch
Modified: 2019-08-21 09:52 PDT (History)
5 users (show)

Obtained from: Built from git using configure
Generated by: ---
Bug category:
Observed for: ---
Regression: ---
Regression since:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description E. C. Masloch 2019-08-20 05:32:32 PDT
I stripped down the lmacros1.mac file to just the definition of the numdef multi-line macro, as well as the defaulting state variable. Any usage of numdef seems to crash NASM. This is with https://repo.or.cz/nasm.git/commitdiff/a1a844697daabe0e8c493c6abada8049431b7fd2

tests$ cat test.asm
%if 0

NASM macro collection
Public Domain by C. Masloch, 2008-2012
Intended for 86 Mode programs.

%endif


                ; Define a numeric definition for conditional assembly
                ;
                ; Instead of:
                ; %ifdef DEFINE
                ; use:
                ; %if _DEFINE
                ;
                ; %1 = Definition name. Will get an additional underscore prefix
                ; %2 = Value to assign if not defined (defaults to 0)
                ; %3 = Value to assign if defined, but no number (defaults to 1)
        %imacro numdef 1-3.nolist 0,1
%ifnum __DEFAULTED_%1
 %if __lMACROS1_MAC__DEFAULTING && __DEFAULTED_%1
  %undef _%1                    ; cause defaulting to take effect again
  %if __DEFAULTED_%1 == 2       ; defaulted to third parameter?
   %define _%1                  ; cause alternative defaulting to take effect again
  %endif
 %endif
%endif
%ifdef _%1
 %ifnnum _%1
  %assign _%1 %3                ; i.e. "-d_DEFINE" option on NASM's command line
  %if __lMACROS1_MAC__DEFAULTING
   %assign __DEFAULTED_%1 2     ; note alternative defaulting occurred
  %endif
 %endif                         ;  (if option was "-d_DEFINE=0", it's left as zero)
 %if __lMACROS1_MAC__DEFAULTING
  %ifnnum __DEFAULTED_%1
   %assign __DEFAULTED_%1 0     ; note no defaulting occurred
  %endif
 %endif
%else                           ; If not defined (no option on NASM's command line)
 %assign _%1 %2                 ;  then assign to zero
 %if __lMACROS1_MAC__DEFAULTING
  %assign __DEFAULTED_%1 1      ; note defaulting occurred
 %endif
%endif
        %endmacro


%assign __lMACROS1_MAC__DEFAULTING 0
        %imacro defaulting 0-1.nolist 1
%ifidni %1,toggle
 %assign __lMACROS1_MAC__DEFAULTING !__lMACROS1_MAC__DEFAULTING
%else
 %assign __lMACROS1_MAC__DEFAULTING !!(%1)
%endif
        %endmacro

        numdef _lMACROS1_MAC__CPU_DEFAULTS
tests$ nasm -v
NASM version 2.15rc0 compiled on Aug 20 2019
tests$ nasm -I ../ test.asm
Segmentation fault (core dumped)
tests$ oldnasm -v
NASM version 2.15rc0 compiled on Dec 28 2018
tests$ oldnasm -I ../ test.asm
tests$
Comment 1 E. C. Masloch 2019-08-20 05:38:55 PDT
Same regardless of what result the numdef should assign:

tests$ nasm test.asm -D__lMACROS1_MAC__CPU_DEFAULTS
Segmentation fault (core dumped)
tests$ nasm test.asm -D__lMACROS1_MAC__CPU_DEFAULTS=1
Segmentation fault (core dumped)
tests$ nasm test.asm -D__lMACROS1_MAC__CPU_DEFAULTS=0
Segmentation fault (core dumped)
tests$
Comment 2 H. Peter Anvin 2019-08-20 16:11:14 PDT
This should be fixed now (ffe89ddaed9c53a4c43946cce1c8e43470f8a284).

While I was adjusting that code I found a trivial way to optimize the case you complained about earlier (an mmacro wrapping an instruction, e.g. CALL), so that is also committed now.

Please confirm if possible that this is addressing your needs. I really, really have appreciated your rapid testing and feedback, it has been invaluable.
Comment 3 E. C. Masloch 2019-08-21 09:52:07 PDT
The bug is fixed as of https://repo.or.cz/nasm.git/commitdiff/7eb18213b78f06b45c85e2b224613cce4a20304b

Note that you have this chunk in https://repo.or.cz/nasm.git/commitdiff/41d91a92732fb1da10059c711078ce05fe43f029 :

@@ -2550,7 +2549,7 @@ static void undef_smacro(const char *mname, bool undefalias)
                         s->in_progress = false;
                     }
                 } else {
-                    if (list_option('m'))
+                    if (list_option('d'))
                         list_smacro_def(s->alias ? PP_UNDEFALIAS : PP_UNDEF,
                                         ctx, s);
                     *sp = s->next;
 

I think you wanted 's' there.

I'll continue the mmacro performance thing in https://bugzilla.nasm.us/show_bug.cgi?id=3392602